﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SSHTool.RRU_QUERY
{
    public partial class FlieUpLoadToLinux : Form
    {
        FileStream fs = null;
        public FlieUpLoadToLinux()
        {
            InitializeComponent();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "D:\\";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Filter = "*.tar|";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txt_localpath.Text = openFileDialog1.FileName;
                try
                {
                    fs = new FileStream(openFileDialog1.FileName, FileMode.Open);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("悲剧: 无法读取磁盘文件！ 原话: " + ex.Message);
                }
            }
            //SaveFileDialog sfdg2 = new SaveFileDialog();
            //if (sfdg2.ShowDialog() == DialogResult.OK)
            //{
            //    string path = sfdg2.FileName;
            //    FileStream 接收文件流 = new FileStream(path, FileMode.CreateNew, FileAccess.Read);
            //    BinaryReader r = new BinaryReader(接收文件流);
            //    byte[] data2 = r.ReadBytes((int)接收文件流.Length);
            //    接收文件流.Write(data2, 0, (int)data2.Length);
            //}
            //else
            //{
            //}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (fs != null)
                {
                    Renci.SshNet.SftpClient ssh_C = new Renci.SshNet.SftpClient(txt_ip.Text, txt_username.Text, txt_pwd.Text);
                    ssh_C.Connect();

                    if (ssh_C.IsConnected)
                    {
                        byte[] 数据 = new byte[fs.Length];
                        fs.Read(数据, 0, (int)fs.Length);

                        ssh_C.UploadFile(fs, txt_rootpath.Text, null);
                        fs.Close();
                        txt_localpath.Text = string.Empty;
                    }

                    MessageBox.Show("文件传输完成");
                }
                else
                {
                    MessageBox.Show("文件读取失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Renci.SshNet.SshClient ssh_C = new Renci.SshNet.SshClient(txt_ip.Text, txt_username.Text, txt_pwd.Text);
            ssh_C.Connect();
            if (ssh_C.IsConnected)
            {
                Renci.SshNet.SshCommand z = ssh_C.RunCommand("cat "+txt_rootpath.Text);
                if (!string.IsNullOrEmpty(z.Error))
                {
                    MessageBox.Show("文件不存在！");
                }
                else
                {
                    MessageBox.Show("文件已存在！");
                }
            }
        }
    }
}
